﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Legend.Component
{
    public class ComponentManager
    {
        private Dictionary<Type, Interface.IComponent> components = new Dictionary<Type,Legend.Component.Interface.IComponent>();

        public bool RegistComponent(Interface.IComponent component)
        {
            Type uniqueType = component.UniqueType;
            if (components.ContainsKey(uniqueType))
            {
                return false;
            }

            components.Add(uniqueType, component);
            return true;
        }

        public void UnRegistComponent<T>()
        {
            UnRegistComponent(typeof(T));
        }
        public void UnRegistComponent(Type t)
        {
            if (components.ContainsKey(t))
            {
                components.Remove(t);
            }
        }

        public T FindComponent<T>() where T : class, Interface.IComponent
        {
            if (!components.ContainsKey(typeof(T)))
            {
                return default(T);
            }
            
            return components[typeof(T)] as T;
        }

        public IEnumerable<Interface.IComponent> GetComponents()
        {
            return components.Values;
        }

        
    }
}
